package com.example.commen.base.dialoglib.dialog;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.example.commen.databinding.DialogMessageBinding;
import com.example.commen.utile.CommonUtils;


/**
 * 进度dialog,仿照IOS样式
 */
public class MegaDevOffOnlineDialog extends Dialog {


    private int backgroundID;
    private String url = "";
    private OnClickListener onClickListener;
    private boolean outIsHind = false;
    private String message = "", title = "";
    private Context context;
    private DialogMessageBinding bind;

    private MegaDevOffOnlineDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }

    public MegaDevOffOnlineDialog(Context context, String title, String message, String url, int backgroundID, OnClickListener onClickListener) {
        this(context, title, message, url, backgroundID, false, onClickListener);
    }

    public MegaDevOffOnlineDialog(Context context, String title, String message, String url, int backgroundID, boolean outIsHind, OnClickListener onClickListener) {
        this(context, com.qmuiteam.qmui.R.style.QMUI_BottomSheet);
        this.message = message;
        this.title = title;
        this.url = url;
        this.backgroundID = backgroundID;
        this.outIsHind = outIsHind;
        this.context = context;
        this.onClickListener = onClickListener;
    }

    public MegaDevOffOnlineDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bind = DialogMessageBinding.inflate(LayoutInflater.from(context));
        setContentView(bind.getRoot());
        initView();
    }

    public MegaDevOffOnlineDialog setBackgroundID(int backgroundID) {
        this.backgroundID = backgroundID;
        return this;
    }

    public MegaDevOffOnlineDialog setMessage(String message) {
        this.message = message;
        return this;
    }

    public MegaDevOffOnlineDialog setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        return this;
    }

    public MegaDevOffOnlineDialog setOutIsHind(boolean outIsHind) {
        this.outIsHind = outIsHind;
        return this;
    }

    public MegaDevOffOnlineDialog setUrl(String url) {
        this.url = url;
        return this;
    }


    private void initView() {
        setCancelable(true);
        this.setCanceledOnTouchOutside(outIsHind);
        //设置其大小
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = CommonUtils.getScreenHeight();
        window.setAttributes(lp);
        CommonUtils.setImage(context, url, bind.imgDialogMsg);
        bind.tvDialogTitle.setText(title);
        bind.tvDialogMsg.setText(message);
        bind.conslayoutDialogMsg.setOnClickListener(v -> {
            if (outIsHind) {
                dismiss();
            }
        });
        bind.btDialogSubmit.setBackgroundResource(backgroundID);
        bind.btDialogSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null)
                    onClickListener.onClick(MegaDevOffOnlineDialog.this, v.getId());
            }
        });

    }
}
